<template>
    <div class="delete-dialog">
      <!--确认删除对话框-->
      <el-dialog
        title="确认删除"
        :visible="dialogVisible"
        width="30%"
      >
        <span>删除题目同时在其关联的试卷中删除</span>
        <span slot="footer" class="dialog-footer">
        <el-button @click="hideDialog">取 消</el-button>
        <el-button type="primary" @click="delQuestions">确 定</el-button>
      </span>
      </el-dialog>
    </div>
</template>

<script>
import { mapState } from 'vuex'
import api from '@/api'
export default {
  computed: {
    ...mapState({
      dialogVisible: ({ question }) => question.delDialogVisible
    })
  },
  methods: {
    // 隐藏对话框
    hideDialog () {
      this.$store.commit('question/changeDelDialog', false)
    },
    // 删除题目
    delQuestions () {
      let isSelected = this.$store.state.question.delSelected
      let ids
      if (isSelected) {
        ids = []
        this.$store.state.question.selectedQuestions.forEach((question, index) => {
          ids[index] = question.id
        })
      } else {
        ids = [this.$store.state.question.curRow.id]
      }
      api.delQuestions(ids)
        .then(api.commonResp((success, data) => {
          if (success) {
            this.$emit('del-success')
            this.hideDialog()
            this.$message('删除成功')
          } else {
            this.$message(data)
          }
          this.hideDialog()
        }))
    }
  }
}
</script>

<style scoped lang="less">

</style>
